﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPMS.Log.Utility;

namespace SPMS.Log.Core
{
    /// <summary>
    /// 日志接口实现
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2009/10/13 14:50    创建
    /// </history>
    public sealed class LogImplement : AbstractLoggerProvider, ILog
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="LogImplement"/>的新实例
        /// </summary>
        /// <param name="logger">日志记录器</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/13 14:57    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/13 14:58    创建
        /// </history>
        public LogImplement( ILogger logger )
            : base( logger )
        {
        }

        #endregion

        #region ---ILog

        /// <summary>
        /// 判断给定的日志级别的日志信息是否允许输出
        /// </summary>
        /// <param name="level">用于判定的日志级别</param>
        /// <returns>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="level"/>为Null</exception>
        /// <history>
        /// [zanezeng]               2009/9/23 9:25    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/13 15:01    创建
        /// </history>
        public bool IsLevelEnabled( Level level )
        {
            return this.Logger.IsLevelEnabled( level );
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出调试级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/23 9:20    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:35    创建
        /// </history>
        public bool IsDebugEnabled
        {
            get
            {
                return this.IsLevelEnabled( Level.Debug );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出跟踪级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/23 9:20    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public bool IsTraceEnabled
        {
            get
            {
                return this.IsLevelEnabled( Level.Trace );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出提示级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/23 9:20    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public bool IsInfoEnabled
        {
            get
            {
                return this.IsLevelEnabled( Level.Info );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出警告级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/23 9:20    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public bool IsWarnEnabled
        {
            get
            {
                return this.IsLevelEnabled( Level.Warn );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出错误级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/23 9:20    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public bool IsErrorEnabled
        {
            get
            {
                return this.IsLevelEnabled( Level.Error );
            }
        }

        /// <summary>
        /// 根据当前日志级别设定判断是否允许输出灾难级别日志信息
        /// </summary>
        /// <value>
        /// 	<c>true</c>：允许输出<br/>
        /// 	<c>false</c>：不允许输出
        /// </value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/23 9:20    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public bool IsFatalEnabled
        {
            get
            {
                return this.IsLevelEnabled( Level.Fatal );
            }
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public void Debug( object message )
        {
            this.WriteLog( Level.Debug, message );
        }

        /// <summary>
        /// 输出调试信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public void Debug( object message, Exception ex )
        {
            this.WriteLog( Level.Debug, message, ex );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public void Trace( object message )
        {
            this.WriteLog( Level.Trace, message );
        }

        /// <summary>
        /// 输出跟踪信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public void Trace( object message, Exception ex )
        {
            this.WriteLog( Level.Trace, message, ex );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:36    创建
        /// </history>
        public void Info( object message )
        {
            this.WriteLog( Level.Info, message );
        }

        /// <summary>
        /// 输出提示信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:38    创建
        /// </history>
        public void Info( object message, Exception ex )
        {
            this.WriteLog( Level.Info, message, ex );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:38    创建
        /// </history>
        public void Warn( object message )
        {
            this.WriteLog( Level.Warn, message );
        }

        /// <summary>
        /// 输出警告信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:38    创建
        /// </history>
        public void Warn( object message, Exception ex )
        {
            this.WriteLog( Level.Warn, message, ex );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:39    创建
        /// </history>
        public void Error( object message )
        {
            this.WriteLog( Level.Error, message );
        }

        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:39    创建
        /// </history>
        public void Error( object message, Exception ex )
        {
            this.WriteLog( Level.Error, message, ex );
        }

        /// <summary>
        /// 输出致命错误信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:39    创建
        /// </history>
        public void Fatal( object message )
        {
            this.WriteLog( Level.Fatal, message );
        }

        /// <summary>
        /// 输出致命错误信息
        /// </summary>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   9/6/2008 9:30 AM    Created
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:39    创建
        /// </history>
        public void Fatal( object message, Exception ex )
        {
            this.WriteLog( Level.Fatal, message, ex );
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">日志的级别</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 9:23    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:39    创建
        /// </history>
        public void WriteLog( Level level, object message )
        {
            this.WriteLog( level, message, null );
        }

        /// <summary>
        /// 输出日志信息
        /// </summary>
        /// <param name="level">日志的级别</param>
        /// <param name="message">需要输出到日志的信息</param>
        /// <param name="ex">需要输出到日志的异常对象</param>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/24 9:23    创建
        /// </history>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/9/30 13:39    创建
        /// </history>
        public void WriteLog( Level level, object message, Exception ex )
        {
            //判断给定的日志级别是否允许输出
            if (!this.IsLevelEnabled( level ))
            {
                //如果给定的日志级别不允许输出，则直接返回
                return;
            }

            //写入日志
            this.Logger.WriteLog( TYPE_STACKBOUNDARY, level, message, ex );
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 堆栈分界类型
        /// </summary>
        private Type TYPE_STACKBOUNDARY = typeof( LogImplement );

        #endregion
    }
}
